package com.gushenge.core.impls

import android.app.Activity
import android.util.Log
import com.gushenge.atools.util.ADate
import com.gushenge.core.*
import com.gushenge.core.base.Base
import com.gushenge.core.beans.*
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.gid
import com.gushenge.core.dao.MMKVConsts.sub
import com.gushenge.core.dao.MMKVConsts.uid
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.models.GameModel
import com.rxlife.coroutine.RxLifeScope
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import rxhttp.awaitResult
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

object GameImpl : GameModel {
    override fun rank(t: Int, type: Int, p: Int, listener: ResultListener) {
        HttpQuery().rank(GlobalConsts.RANK, t, type, p, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Codes<Game>?> {
                override fun onFailure(call: Call<Codes<Game>?>, t: Throwable) {
//                listener.error(t.toString())
                }

                override fun onResponse(call: Call<Codes<Game>?>, response: Response<Codes<Game>?>) {
                    response.body() ?: listener.error("查询失败")
                    response.body()?.let {
                        when (it.code) {
                            1 -> listener.success(it.data, it.p, it.max_p)
                            else -> listener.error(it.message)
                        }
                    }
                }
            })
    }


    override fun getGift(id: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().getGift(GlobalConsts.GET_GIFT, uid, id, time, sign, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Code<String>?> {
                override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
                }

                override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                    response.body() ?: listener.error("领取失败")
                    response.body()?.let {
                        when (it.code) {
                            1 -> listener.success(it.data ?: "未生成礼包码,请重新领取")
                            else -> listener.error(it.message)
                        }
                    }
                }
            })
    }

    override fun gameDetailDeal(p: Int, listener: ResultListener) {
        HttpQuery().gameDetailDeal(GlobalConsts.GAME_DETAIL_DEAL, gid, p, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Codes<Deal>?> {
                override fun onFailure(call: Call<Codes<Deal>?>, t: Throwable) {
                    listener.error()
                }

                override fun onResponse(call: Call<Codes<Deal>?>, response: Response<Codes<Deal>?>) {
                    response.body() ?: listener.error()
                    response.body()?.let {
                        when (it.code) {
                            1 -> listener.success(it.data, it.p, it.max_p)
                            else -> listener.error()
                        }
                    }
                }
            })
    }

    override fun gameDetailServer(p: Int, listener: ResultListener) {
        HttpQuery().gameDetailServer(GlobalConsts.GAME_DETAIL_SERVER, gid, p, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Codes<Server>?> {
                override fun onFailure(call: Call<Codes<Server>?>, t: Throwable) {
                    listener.error()
                }

                override fun onResponse(call: Call<Codes<Server>?>, response: Response<Codes<Server>?>) {
                    response.body() ?: listener.error()
                    response.body()?.let {
                        when (it.code) {
                            1 -> listener.success(it.data, it.p, it.max_p)
                            else -> listener.error()
                        }
                    }
                }
            })
    }

    override fun gameDetailTab(listener: ResultListener) {
        HttpQuery().gameDetail(GlobalConsts.GAME_DETAIL, gid, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Code<GameDetail>?> {
                override fun onFailure(call: Call<Code<GameDetail>?>, t: Throwable) {
//                listener.error(t.toString())
                }

                override fun onResponse(call: Call<Code<GameDetail>?>, response: Response<Code<GameDetail>?>) {
                    response.body() ?: listener.error("查询失败")
                    response.body()?.let {
                        when (it.code) {
                            1 -> {
                                it.data ?: listener.error(it.message)
                                it.data?.let {
                                    listener.success(it)
                                }
                            }

                            else -> listener.error(it.message)
                        }
                    }

                }
            })
    }

    override fun gameDetailTop(listener: ResultListener) {
        HttpQuery().gameDetailTop(GlobalConsts.GAME_DETAIL_TOP, uid, gid, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Code<Game>?> {
                override fun onFailure(call: Call<Code<Game>?>, t: Throwable) {
//                listener.error(t.toString())
                }

                override fun onResponse(call: Call<Code<Game>?>, response: Response<Code<Game>?>) {
                    response.body() ?: listener.error("查询失败")
                    response.body()?.let {
                        when (it.code) {
                            1 -> {
                                it.data ?: listener.error()
                                it.data?.let {
                                    listener.success(it)
                                }
                            }

                            else -> listener.error(it.message)
                        }
                    }

                }
            })
    }

    override fun sortGame(type: Int, p: Int, listener: ResultListener) {
        HttpQuery().sortGame(GlobalConsts.SORT_GAME, type, p, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<CodeSort<Game>?> {
                override fun onFailure(call: Call<CodeSort<Game>?>, t: Throwable) {
//                t.message?.let {
//                    listener.error(it)
//                }
                }

                override fun onResponse(call: Call<CodeSort<Game>?>, response: Response<CodeSort<Game>?>) {
                    response.body()?.let {
                        when (it.code) {
                            1 -> listener.success(it, it.p, it.max_p)
                            else -> listener.error(it.message)
                        }
                    }
                }
            })
    }

    override fun sort(listener: ResultListener) {
        HttpQuery().sort(GlobalConsts.SORT, MMKVConsts.sub, MMKVConsts.language)
            .enqueue(object : Callback<Codes<Sort>?> {
                override fun onFailure(call: Call<Codes<Sort>?>, t: Throwable) {
//                t.message?.let {
//                    listener.error(it)
//                }
                }

                override fun onResponse(call: Call<Codes<Sort>?>, response: Response<Codes<Sort>?>) {
                    response.body()?.let {
                        when (it.code) {
                            1 -> listener.success(it.data)
                            else -> it.message.let { listener.error(it) }
                        }
                    }
                }
            })
    }

    fun gameDetailTab1(listener: ResultListener) {
        HttpQuery().gameDetail1(GlobalConsts.GAME_CONTENT, gid, sub).enqueue(object : Callback<GameBottomDetail?> {
            override fun onFailure(call: Call<GameBottomDetail?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<GameBottomDetail?>, response: Response<GameBottomDetail?>) {
                response.body() ?: listener.error("查询失败")
                response.body()?.let {

                    when (it.code) {
                        1 -> {
                            listener.success(it)
                        }

                        else -> listener.error(it.message)
                    }
                }

            }
        })
    }

    fun getGameInfoTop(id: String?, listener: GameTopDetail.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.get(GlobalConsts.GAMEINFOTOP)
                .add("id", id)
                .add("member_id", sub)
                .add("uid", uid)
                .toClass<GameTopDetail>()
                .awaitResult {
                    if (it.code == 1) {
                        listener(it)
                    } else {
                        it.message?.toast()
                    }
                }

        }


    }

    /**
     * id 游戏id
     * content用户评论
     * point	评分1-10
     * pid评论的id
     */
    fun pushPingLun(
        activity: Activity,
        id: String,
        images: ArrayList<String>?,
        video: String?,
        content: String,
        point: String,
        pid: String? = "0",
        ppid: String? = "0",
        type: Int?,
        pinglun: Pinglun?,
        listener: Boolean.() -> Unit
    ) {
        val time1 = time
        RxLifeScope().launch {
            RxHttp.get(GlobalConsts.PINGLUN)
                .add("uid", uid)
                .add("t", time1)
                .add("sign", httpSign(uid + time1))
                .add("id", id)
                .add("content", content)
                .add("member_id", sub)
                .add("video", video)
                .add("images", images)
                .add("point", point)
                .add("pid", pid)
                .add("ppid", ppid)
                .toClass<Code<String>>()
                .awaitResult {
                    if (it.code == 1) {
                        it.message.toast()
                        listener(true)
                        if (type == 0) {

                            activity.finish()
                        } else if (type == 1) {
//                            CommentDetailActivity.start(activity,pinglun)
                            activity.finish()
                        }
                    } else {
                        listener(false)
                        it.message.toast()
                    }
                }.onFailure {
                    listener(false)
                    "发表评论失败".toast()
                }
        }
    }

    /**
     *评论列表
     */
    fun getPingLun(listener: ArrayList<Pinglun>?. () -> Unit) {
        RxLifeScope().launch {
            RxHttp.get(GlobalConsts.PINGLUN_LIST)
                .add("uid", uid)
                .add("id", gid)
                .add("member_id", sub)
                .toClass<Codes<Pinglun>>()
                .awaitResult {
                    if (it.code == 1) {
                        listener(it.data)
                    } else {
                        listener(null)
//                        it.message.toast()
                    }
                }.onFailure {
                    listener(null)
                    Log.e("TAG", "getPingLun: ${it.localizedMessage}")
                }
        }
    }

    /**
     * 子评论
     */
    fun getChildPingLun(id: String, pid: String = "0", listener: ArrayList<Pinglun>?. () -> Unit) {
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.PINGLUN_CHILD_LIST)
                .add("uid", uid)
                .add("id", id)
                .add("member_id", sub)
                .add("pid", pid)
                .toClass<Codes<Pinglun>>()
                .awaitResult {
                    if (it.code == 1) {
                        listener(it.data)
                    } else {
                        listener(null)
//                        it.message.toast()
                    }
                }.onFailure {
                    listener(null)
                    Log.e("TAG", "getPingLun: ${it.localizedMessage}")
                }
        }
    }

    /**
     * 点赞
     */
    fun pingLunZan(id: String, listener: Boolean.() -> Unit) {
        val time1 = time
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.PINGLUN_ZAN)
                .add("uid", uid)
                .add("id", id)
                .add("member_id", sub)
                .add("t", time1)
                .add("sign", httpSign(uid + time1))
                .toClass<Code<String>>()
                .awaitResult {
                    it.message.toast()
                    if (it.code == 1) {
                        listener(true)
                    } else {
                        listener(false)
                    }
                }.onFailure {
                    listener(false)
                    Log.e("TAG", "getPingLun: ${it.localizedMessage}")
                }
        }
    }

    /**
     * 删除评论
     */
    fun dellPingLun(id: String, listener: Boolean.() -> Unit) {
        val time1 = time
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.DELL_PINGLUN)
                .add("uid", uid)
                .add("id", id)
                .add("member_id", sub)
                .add("t", time1)
                .add("sign", httpSign(uid + time1))
                .toClass<Code<String>>()
                .awaitResult {
                    it.message.toast()
                    if (it.code == 1) {
                        listener(true)
                    } else {
                        listener(false)
                    }
                }.onFailure {
                    listener(false)
                    Log.e("TAG", "getPingLun: ${it.localizedMessage}")
                }
        }
    }

    fun getTradeConfig(listener: TradeConfig?.() -> Unit) {
        RxLifeScope().launch {
            val time1 = time
            RxLifeScope().launch {
                RxHttp.get(GlobalConsts.TRADE_CONFIG)
                    .add("uid", uid)
                    .add("member_id", sub)
                    .add("t", time1)
                    .add("sign", httpSign(uid))
                    .toClass<Code<TradeConfig>>()
                    .awaitResult {
                        if (it.code == 1) {
                            listener(it.data)
                        } else {
                            listener(null)
                        }
                    }.onFailure {
                        listener(null)
                    }
            }
        }
    }


    // 游戏系逆袭
    fun getGameInfo(id: String?, listener: GameTopDetail.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_GAME_INFO)
                .add("id", id)
                .toClass<BaseResult<GameTopDetail>>()
                .awaitResult {
                    if (it.code == 1) {
                        listener(it.data!!)
                    } else {
                        it.message.toast()
                    }
                }

        }
    }

    fun getGameRole(id: String?, listener: ArrayList<GameGoodsModel>.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_GAME_ROLE)
                .add("id", id)
                .add("uid", uid)
                .add("member_id", sub)
                .add("t", time)
                .add("sign", httpSign(uid))
                .toClass<BaseResult<ArrayList<GameGoodsModel>>>()
                .awaitResult {
                    if (it.code == 1) {
                        listener(it.list!!)
                        it.msg.toast()
                    } else {
                        it.message.toast()
                        it.msg.toast()
                    }
                }

        }
    }

    // 提交激活码
    fun submitCode(gm_vip_id: String, code: String, listener: DelResult.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_CODE)
                .add("uid", uid)
                .add("gm_vip_id", gm_vip_id)
                .add("code", code)
                .add("t", time)
                .add("sign", httpSign(uid))
                .toClass<DelResult>()
                .awaitResult {
                    listener(it)
                }

        }
    }

    // 充值
    fun submitMoneyCharge(
        shouyou_id: String,
        role: String,
        region_id: String? = null,
        money: String? = null,
        listener: DelResult.() -> Unit
    ) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_SUBMIT_CHARGE)
                .add("uid", uid)
                .add("shouyou_id", shouyou_id)
                .add("role", role)
                .add("region_id", region_id)
                .add("money", money)
                .add("req_type", "1")
                .add("t", time)
                .add("sign", httpSign(uid))
                .toClass<DelResult>()
                .awaitResult {
                    listener(it)
                }

        }
    }

    // 物品充值
    fun submitGoodsCharge(
        shouyou_id: String,
        role: String,
        region_id: String? = null,
        goods_id: String? = null,
        goods_num: String? = null,
        listener: DelResult.() -> Unit
    ) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_SUBMIT_CHARGE)
                .add("uid", uid)
                .add("shouyou_id", shouyou_id)
                .add("role", role)
                .add("region_id", region_id)
                .add("goods_id", goods_id)
                .add("goods_num", goods_num)
                .add("req_type", "2")
                .add("t", time)
                .add("sign", httpSign(uid))
                .toClass<DelResult>()
                .awaitResult {
                    listener(it)
                }

        }
    }

    // 删除游戏
    fun delGame(shouyou_id: String, listener: DelResult.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_BIND_DEL)
                .add("uid", uid)
                .add("shouyou_id", shouyou_id)
                .add("t", time)
                .add("sign", httpSign(uid))
                .toClass<DelResult>()
                .awaitResult {
                    listener(it)
                }

        }
    }

    // 获取已绑定列表
    fun getBindGameList(type: Int, listener: ArrayList<BindGameBean>.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_BIND_GM)
                .add("type", type)
                .add("uid", uid)
                .toClass<Codes<BindGameBean>>()
                .awaitResult {
                    if (it.code == 1) {
                        listener(it.list)
                        it.msg.toast()
                    } else {
                        it.message.toast()
                        it.msg.toast()
                    }
                }

        }
    }

    // 绑定角色
    fun submitRole(shouyou_id: String, role: String, listener: String.() -> Unit) {
        RxLifeScope().launch {
            RxHttp.postForm(GlobalConsts.GM_SUBMIT_ROLE)
                .add("uid", uid)
                .add("role", role)
                .add("shouyou_id", shouyou_id)
                .add("t", time)
                .add("sign", httpSign(uid))
                .toClass<BaseResult<String>>()
                .awaitResult {
                    if (it.code == 1) {
                        it.msg.toast()
                        listener(it.data!!)
                    } else {
                        it.msg.toast()
                    }
                }

        }
    }
}